#include "game.h"

Game::Game (QWidget *parent, int cardNumber)
{
	widget = new QWidget(parent);
	gridLayout = new QGridLayout();
	widget->setLayout(gridLayout);

	createIcons(cardNumber);
	iconsMix();
	createCards(cardNumber);
	displayCards();
}

Game::~Game()
{
	delete gridLayout;
	for(int i=0; i<cards.size(); i++)
	{
		delete cards[i];
	}
	delete widget;
}

	void Game::createIcons(int iconNumber)
	{
		QString s;
		for(int i = 1; i <= iconNumber/2; i++)
		{
			s=("Resources/Pictures/"+ QString::number(i)+ ".jpg");    
			icons.push_back(QIcon(s));
			icons.push_back(QIcon(s));
		}
	}

	void Game::iconsMix()
	{
	qsrand(QTime::currentTime().msec());
	QVector <QIcon> tempIcons;

	int t, s;

	for (int i=icons.size(); i>0; i--)
	{
		t = (qrand() % i) + 1; //losujemy z i elementow od 1
		s= t-1;
		tempIcons.push_back(icons[s]);
		icons.erase(icons.begin() + s);
	}

	icons = tempIcons;
	}

	void Game::createCards(int cardNumber)
	{
		for(int i = 0; i < cardNumber; i++)
		{
			cards.push_back(new Card());
			cards[i]->setAverse(icons[i]);
			cards[i]->setReverse(QIcon("Resources/Pictures/paleta.jpg"));
		}
	}

	void Game::displayCards()
	{
		int k=0;
		for(int i=0; i<(qSqrt(cards.size())); i++)
		{
			for(int j=0; j<(qSqrt(cards.size())); j++)
			{
				cards[k]->getButton()->setIcon(cards[k]->getReverse());
				gridLayout->addWidget(cards[k]->getButton(),i,j);
				k++;
			}
		}

	}

